import { chartEncaps } from './chartEncaps.js'

let drawb7 = obj => {
  // obj = {
  //   _t: this,
  //   doms: 'mycharts
  // }
  obj._t.bjTotal = chartEncaps.setTjData({ _t: obj._t })
  let max = 0
  obj._t.chartData.forEach(ob => {
    if (ob.value > max) {
      max = ob.value
    }
  })

  // 自定义 other 的图形，增加两条线
  obj._t.$registerShape('interval', 'slice-shape', {
    draw(cfg, container) {
      const points = cfg.points
      const origin = cfg.data
      const percent = origin.value / max
      const xWidth = points[2].x - points[1].x
      const width = xWidth * percent
      let path = []
      path.push(['M', points[0].x, points[0].y])
      path.push(['L', points[1].x, points[1].y])
      path.push(['L', points[0].x + width, points[2].y])
      path.push(['L', points[0].x + width, points[3].y])
      path.push('Z')
      path = this.parsePath(path)
      return container.addShape('path', {
        attrs: {
          fill: cfg.color,
          path
        }
      })
    }
  })
  let w = obj._t.$refs.drawCharts.offsetWidth
  // let pad = { padding: 'auto', autoFit: true }
  switch (obj._t.chartsConData.legend) {
    case '0':
      chartEncaps.drawPublic({
        _t: obj._t,
        doms: obj.doms,
        data: { padding: 'auto', autoFit: false, width: w, height: 685 }
      })
      break
    case '1':
      chartEncaps.drawPublic({
        _t: obj._t,
        doms: obj.doms,
        data: { padding: 'auto', autoFit: false, width: w, height: 685 }
      })
      break
  }
  // chartEncaps.drawPublic({
  //     _t: obj._t,
  //     doms: obj.doms,
  //     data: pad
  // })
  chartEncaps.setDisplay({ doms: obj.doms, display: 'table' })
  chartEncaps.setLegend({ _t: obj._t, data: 'label' })
  obj._t.chart.coordinate('theta', {
    radius: 0.8
  })
  let dv = chartEncaps.setPercent({
    _t: obj._t,
    data: chartEncaps.setPercentValue({ _t: obj._t })
  })
  if (dv.length > 0) {
    obj._t.chart.data(dv)
    obj._t.chart.scale('percent', {
      formatter: val => {
        val = val + '%'
        return val
      }
    })
    obj._t.chart.tooltip({
      showTitle: false,
      showMarkers: false
    })
    let position
    let content
    switch (obj._t.chartsConData.percent) {
      case '1':
        position = 'percent'
        content = data => {
          return data['label'] + ':' + data['percent'] + '%'
        }
        break
      case '0':
        position = 'value'
        content = data => {
          return data['label'] + ':' + data['value']
        }
        break
      case '3':
        position = 'percent'
        content = data => {
          return (
            data['label'] +
            ':' +
            data['value'] +
            '，' +
            data['percent'].toFixed(obj._t.chartsConData.precision) +
            '%'
          )
        }
        break
    }
    // let { offset, offsetX, offsetY } = chartEncaps.setOffset({ _t: obj._t })
    let m = 0

    obj._t.chart
      .interval()
      .adjust('stack')
      .position(position)
      .color('label', obj._t.color)
      .shape('slice-shape')
      .label('label', val => {
        m = m + 1
        return {
          offset: obj._t.chartsConData.offset,
          content: data => {
            return content(data)
          },
          style: {
            fill: '#555', // 文本的颜色
            stroke: '',
            fontSize: obj._t.chartsConData.textSize // 文本大小
          }
        }
      })
    // obj._t.chart.interaction('element-active')

    obj._t.chart.render()
  }
  obj._t.setLoading(false)
  obj._t.setshadeDisplay(true)
}

export { drawb7 }
